package com.neusoft.restaurant.controller;

import com.neusoft.restaurant.config.utils.R;
import com.neusoft.restaurant.entity.PageResult;
import com.neusoft.restaurant.entity.ResOrders;
import com.neusoft.restaurant.entity.ResOrdersDetail;
import com.neusoft.restaurant.service.IResOrdersDetailService;
import com.neusoft.restaurant.service.IResOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 * 
 * @author zhongda
 * @date 2023-12-10
 */
@RestController
@RequestMapping("/api/orders")
public class ResOrdersController extends BaseController
{
    @Autowired
    private IResOrdersService resOrdersService;

    @Autowired
    private IResOrdersDetailService resOrdersDetailService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public R list(ResOrders resOrders)
    {
        List<ResOrders> list = resOrdersService.selectResOrdersList(resOrders);
        ResOrdersDetail detail =new ResOrdersDetail();

        for (ResOrders orders:list) {
            detail.setOrderId(orders.getId());
           List<ResOrdersDetail> detailList= resOrdersDetailService.selectResOrdersDetailList(detail);
           orders.setOrdersDetails(detailList);
        }
        return R.success(list);
    }

    @GetMapping("/list2")
    public R list2(ResOrders resOrders,Integer pageNum,Integer pageSize)
    {
        PageResult list = resOrdersService.selectResOrdersList2(resOrders,pageNum,pageSize);
        return R.success(list);
    }


    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {
        return R.success(resOrdersService.selectResOrdersById(id));
    }

    /**
     * 新增订单
     */
    @PostMapping
    public R add(@RequestBody ResOrders resOrders)
    {
        System.out.println("订单提交数据----->"+resOrders.toString());
        return R.success(resOrdersService.insertResOrders(resOrders));
    }

    /**
     * 修改订单
     */
    @PutMapping
    public R edit(@RequestBody ResOrders resOrders)
    {
        return R.toAjax(resOrdersService.updateResOrders(resOrders));
    }

    /**
     * 删除订单
     */
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        return R.toAjax(resOrdersService.deleteResOrdersByIds(ids));
    }
}
